from bs4 import BeautifulSoup
import requests
 
# 发送请求获取网页内容
url = 'https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E5%A5%BD%E5%A3%B0%E9%9F%B3%E7%AC%AC%E4%B8%80%E5%AD%A3/16843195'
response = requests.get(url)

 
# 打印网页内容
soup = BeautifulSoup(response.text, 'html.parser')

tables = soup.find_all('table')

if tables:
    first_table = tables[1]
    links = first_table.find_all('a')
    print("\n第一个表格中的链接:")
    for link in links:
            href = link.get('href')  # 获取链接的地址
            text = link.text  # 获取链接的文本
            print(f"链接文本: {text}, 链接: {href}")
            if text == "黄鹤":
                # 完整的链接地址
                complete_link = f"https://baike.baidu.com{href}"  # 根据链接的相对路径构造完整URL
                response1 = requests.get(complete_link)
                soup1 = BeautifulSoup(response1.text, 'html.parser')
                abstract_album_div = soup.find('div', class_='abstractAlbum xa80B')
                title1 = soup1.title.text
                print('网页标题:', title1)
                img_tags = soup1.find_all('img')

else:
    print("没有找到表格。")


title = soup.title.text
print('网页标题:', title)

